package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.CnasSortNameMapper;
import com.zhijian.medical.entity.po.CnasSortName;
import com.zhijian.medical.entity.vo.request.CnasSortNameSearchVo;
import com.zhijian.medical.entity.vo.response.CnasSortNameVo;
import com.zhijian.medical.service.CnasSortNameService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * CNAS认可的具体低级序号名称业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-03-07 16:37:16
 */
@Slf4j
@Service
public class CnasSortNameServiceImpl implements CnasSortNameService {

    @Autowired
    private CnasSortNameMapper mapper;

    @Override
    public CnasSortName selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public CnasSortNameVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(CnasSortName cnasSortName) {
        Date now = new Date();
        cnasSortName.setCreateTime(now);
        cnasSortName.setUpdateTime(now);
        return mapper.insert(cnasSortName) > 0 ? cnasSortName.getId() : 0L;
    }

    @Override
    public boolean update(CnasSortName cnasSortName) {
        Date now = new Date();
        cnasSortName.setUpdateTime(now);
        return mapper.updateByPrimaryKey(cnasSortName) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(CnasSortName record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(CnasSortNameSearchVo cnasSortNameSearchVo) {
        return mapper.count(cnasSortNameSearchVo);
    }

    @Override
    public List<CnasSortNameVo> selectList(CnasSortNameSearchVo cnasSortNameSearchVo) {
        return mapper.selectList(cnasSortNameSearchVo);
    }

    @Override
    public Page<CnasSortNameVo> selectPage(CnasSortNameSearchVo cnasSortNameSearchVo) {
        //先直接查总数
        int count = count(cnasSortNameSearchVo);
        //分页
        Page<CnasSortNameVo> page = new Page<>(true, count, cnasSortNameSearchVo.getPageNum(), cnasSortNameSearchVo.getPageSize());
        cnasSortNameSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<CnasSortNameVo> responseVos = this.selectList(cnasSortNameSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<CnasSortName> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public List<CnasSortNameVo> renderTreeList(String ysSort) {
        List<CnasSortNameVo> cnasSortNameVos = this.selectList(CnasSortNameSearchVo.builder()
                .ysSort(ysSort)
                .build());
        Collections.reverse(cnasSortNameVos);
        return selectTreeList(cnasSortNameVos, 0L);
    }

    @Override
    public List<CnasSortNameVo> selectTreeList(List<CnasSortNameVo> all, Long pid) {
        return createTree(all, pid);
    }

    /**
     * 生成树的方法
     */
    private List<CnasSortNameVo> createTree(List<CnasSortNameVo> list, Long pid) {
        try {
            //根节点
            List<CnasSortNameVo> tempList = new ArrayList<>();
            for (CnasSortNameVo locationVo : list) {
                //父节点是0的，为根节点。
                if (Objects.equals(locationVo.getParentId(), pid)) {
                    tempList.add(locationVo);
                }
            }
            //为根id设置子childList是递归调用的
            for (CnasSortNameVo locationVo : tempList) {
                /* 获取根节点下的所有子节点 使用getChild方法*/
                List<CnasSortNameVo> childList = setChild(locationVo.getId(), list);
                //给根节点设置子节点
                locationVo.setChildren(childList);
            }
            // 输出构建好的菜单数据。
            return tempList;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    /**
     * 获取子菜单
     * 获取子节点
     *
     * @param id   父节点id
     * @param list 所有节点列表
     * @return 每个根节点下，所有子节点列表
     */
    private List<CnasSortNameVo> setChild(Long id, List<CnasSortNameVo> list) {
        //子节点
        List<CnasSortNameVo> childList = new ArrayList<>();
        for (CnasSortNameVo locationVo : list) {
            // 遍历所有节点，将所有子节点的父id与传过来的根节点的id比较
            //相等说明：为该根节点的子节点。
            if (locationVo.getParentId().equals(id)) {
                childList.add(locationVo);
            }
        }
        //递归
        for (CnasSortNameVo locationVo : childList) {
            locationVo.setChildren(setChild(locationVo.getId(), list));
        }
        //如果节点下没有子节点，返回一个空List（递归退出）
        if (childList.isEmpty()) {
            return new ArrayList<>();
        }
        return childList;
    }

}